package com.godo.test.downfile;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class BaiduImageDown extends DownImage {

	@Override
	public ArrayList<String> getUrls(String url, String code)
			throws IOException {
		ArrayList<String> list = new ArrayList<String>() ;
		String content = readUrlContent(url, code,5000) ;
		String rege = "\"pageNum\":\\d+,\"objURL\":\"(http://(.*?)\\.(jpg|JPG|jpeg|JPEG)\\s+)\"," ;
		Pattern p = Pattern.compile(rege); 
//		用Pattern类的matcher()方法生成一个Matcher对象 
		Matcher m = p.matcher(content); 
		while(m.find()){
			list.add(m.group(1).trim()) ;
		}
		return list ;
	}

	@Override
	public void downImageFromUrl(String url, File outDir)
			throws InterruptedException, IOException {
		System.out.println(outDir +"\t"+url) ;
		List<String> list = getUrls(url, "gbk") ;
		for(int i=0;i<list.size();i++){
			System.err.println(i+"\t"+list.get(i)) ;
			super.downImage(list.get(i), outDir ) ;
		}
	}
	
	@Override
	public File getOutDir(String url, File outDir) {
		String cls = "images";
		int start = url.indexOf("&word=")+6 ;
		if(start >0){
			cls = url.substring(start) ;
			int end = cls.indexOf("&") ;
			int end2 = cls.indexOf("#") ;
			if(end==-1 || end2==-1){
				end = end>end2?end:end2 ;
			} else {
				end = end>end2?end2:end ;
			}
			if(end>0){
				cls = cls.substring(0,end) ;
			}
			try {
				cls = java.net.URLDecoder.decode(cls,"gbk") ;
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd HHmm");
		return new File(outDir,cls+" "+sf.format(new Date()));
	}

	
	/**
	 * @param args
	 * @throws IOException 
	 * @throws InterruptedException 
	 */
	public static void main(String[] args) throws IOException, InterruptedException {
		BaiduImageDown down = new BaiduImageDown() ;
		String url = "http://image.baidu.com/i?tn=baiduimage&ct=201326592&lm=-1&cl=2&word=%C1%D6%D2%C0%B3%BF#tab=1";
		String out = "e:/data/img/";
		

		File dir = down.getOutDir(url,new File(out)) ;
		for(int i=0;i<5;i++){
			down.downImageFromUrl(url+"&pn="+(i*18),dir ) ;
		}
		
	}


}
